﻿using Engine;
using Game.NetWork.Packages;
using Game.NetWork;
using Game.Server.Event;
using Game.Server.PlayerEvent;
using System;
using Game.IContentReader;
using System.Text;
using System.Collections.Generic;
using GameEntitySystem;
using System.Linq;
using static Game.NetWork.Packages.PlayerDataPackage;

namespace Game.Server.Plugins
{
    public class EMMMZHPlugin : ServerPlugin, ICreatureSpawnEventHandle, ICreatureHealthEventHandle
    {
        public override int Version => 10000;

        public override string Name => "好像没用的插件";

        public byte FirstLevel => 0;

        public static Dictionary<string, float> DiePlayerList = new Dictionary<string, float>();


        public override void Initialize()
        {
            CreatureSpawnEventManager.AddObject(this);
            CreatureHealthEventManager.AddObject(this);
        }

        public override void Load()
        {
        }

        public override void Save()
        {
        }

        public bool Update(SubsystemCreatureSpawn subsystemCreatureSpawn, float dt)
        {
            return true;
        }

        public void InitCreatureTypes(SubsystemCreatureSpawn subsystemCreatureSpawn, List<SubsystemCreatureSpawn.CreatureType> creatureTypes)
        {
        }

        public void OnEntityAdded(SubsystemCreatureSpawn subsystemCreatureSpawn, Entity entity)
        {
            ComponentPlayer componentPlayer = entity.FindComponent<ComponentPlayer>();
            if (componentPlayer != null)
            {
                if (DiePlayerList.ContainsKey(componentPlayer.PlayerGuid.ToString()))
                {
                    float level = DiePlayerList[componentPlayer.PlayerGuid.ToString()];
                    foreach (IInventory item in entity.FindComponents<IInventory>())
                    {
                        item.DropAllItems(Vector3.Zero);
                    }
                    if (componentPlayer.ComponentHealth != null && componentPlayer.PlayerData.Level >= level && level > 1f)
                    {
                        CommonLib.Net.QueuePackage(new ComponentPlayerPackage(componentPlayer.PlayerData, 0, false, 0.1f));
                        GameManager.Project.FindSubsystem<SubsystemGameWidgets>().AddMessage($"{componentPlayer.PlayerData.Name} 尝试卡退作弊！");
                        Log.Information($"{componentPlayer.PlayerGuid} {componentPlayer.PlayerData.Name} 尝试卡退作弊！");
                        //componentPlayer.ComponentHealth.Injure(1f, null, true, "尝试作弊被死神带走! ");
                    }
                    DiePlayerList.Remove(componentPlayer.PlayerGuid.ToString());
                }
            }
        }

        public void OnEntityRemoved(SubsystemCreatureSpawn subsystemCreatureSpawn, Entity entity)
        {
        }

        public void OnPlayerSpawned(PlayerData playerData, Entity playerEntity, Vector3 position)
        {
        }

        public bool Heal(ComponentHealth componentHealth, float amount)
        {
            return true;
        }

        public bool Injure(ComponentHealth componentHealth, float amount, ComponentCreature attacker, string cause)
        {
            ComponentPlayer componentPlayer = componentHealth.Entity.FindComponent<ComponentPlayer>();
            if (componentPlayer != null && componentHealth.Health > 0 && componentHealth.Health - amount <= 0)
            {
                if (!DiePlayerList.ContainsKey(componentPlayer.PlayerGuid.ToString()))
                {
                    DiePlayerList.Add(componentPlayer.PlayerGuid.ToString(), componentPlayer.PlayerData.Level);
                }
            }
            return true;
        }
    }
}